﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using 机器组和机器编辑.modify;

namespace 机器组和机器编辑
{
    public partial class ModifyTheMachine : Form
    {
        List<Machine> machines;
        public ModifyTheMachine()
        {
            InitializeComponent();
            Machine machine = new Machine();
        }

        public void ModifyMachineById(int groupId) //修改实现
        {
  
            // 这里从XML加载所有
            machines = LoadMachineXml();
            Machine group = machines.SingleOrDefault(x => x.ID == groupId);
            if (group != null)
            {
                checkBox3.Checked = group.Display; //显示
                textBox5.Text = group.ID.ToString();//ID(仅可读)
                textBox2.Text = group.Name;//机器名称
                textBox1.Text = group.Numbr;//机器编号
                comboBox1.SelectedItem = group.ModelNumder; //机器型号_编号
                checkBox2.Checked = group.Online;//在线
                comboBox2.Text = group.Group;//机器组名称
                checkBox1.Checked = group.Configuration;//配置项

            }
            else
            {
                MessageBox.Show($"未找到对应的机器数据[109]");
            }
        }


        // 从XML加载
        private List<Machine> LoadMachineXml()
        {


            var list = new List<Machine>();
            var doc = XDocument.Load("C:\\Users\\24929\\Desktop\\机器组和机器编辑\\Config\\MachineConfig.xml");


            foreach (var xe in doc.Root.Elements("Machine"))
            {
                list.Add(Machine.FromXElement(xe));
            }
            return list;
        }


        private void ModifyTheMachine_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e) //确定
        {
            if (machines == null) machines = LoadMachineXml();

            int groupId = int.Parse(textBox5.Text);
            Machine group = machines.FirstOrDefault(x => x.ID == groupId);
            if (group != null)
            {
                group.Display = checkBox3.Checked;
                group.Name = textBox2.Text;
                group.Numbr = textBox1.Text;
                group.ModelNumder = comboBox1.Text;
                group.Online = checkBox1.Checked;
                group.Group = comboBox2.Text;
                group.Configuration = checkBox1.Checked;

                SaveMachineGroupsToXml(machines);
                MessageBox.Show("保存成功！");
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("未找到对应的机器组数据！");
            }
        }
        //保存到 XML 文件
        private void SaveMachineGroupsToXml(List<Machine> groups)
        {
            var doc = new XDocument(
                new XElement("Machine",
                    groups.Select(g => g.ToElement())
                )
            );
            doc.Save("C:\\Users\\24929\\Desktop\\机器组和机器编辑\\Config\\MachineConfig.xml");
        }

        private void button2_Click(object sender, EventArgs e)//取消
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
